Компиляция дерева в делегатExpression
    .Lambda(expressionValiarble)
    .Compile();
Простое копирование свойств одного класса в другой 
Маппинг на основе выражения доступа к свойству 
Вызов делегата при построении EpressionTreeExpression Trees and Invoking a Delegate
https://stackoverflow.com/questions/2215712/expression-trees-and-invoking-a-delegate
Объявлений локальной переменной в блоке
(без передачи извне)

переменная типа указана из области видимости, но не определена 
https://question-it.com/questions/717473/peremennaja-tipa-ukazana-iz-oblasti-vidimosti-no-ne-opredelena

 
Типизированый вызов конструктораvar constructorInfo = type.GetConstructors().First(e => e.Attributes.HasFlag(MethodAttributes.Public));
var constructorParameter1 = Expression.Parameter(typeof(bool));
var constructorExp = Expression.New(constructorInfo, constructorParameter1);

// Func<bool, object> or Func<bool, T>
var constructorDelegate = Expression
    .Lambda<Func<bool, object>>(
        constructorExp,
        constructorParameter1
        )
    .Compile();
Нетипизированый вызов
public delegate object ConstructorDelegate(object[]? Args);

public static ConstructorDelegate GetConstructorWithArgsDelegate(
    ConstructorInfo constructorInfo)
{
 ParameterExpression constructorParameters = Expression.Parameter(typeof(object?[]));

 Expression[] parameterExpressions;
 {
  ParameterInfo[] parametersInfos = constructorInfo.GetParameters();
  parameterExpressions = new Expression[parametersInfos.Length];

 for (int i = 0; i < parametersInfos.Length; i++)
  {
   ConstantExpression ithIndex = Expression.Constant(i);
   BinaryExpression ithParameter = Expression.ArrayIndex(
    constructorParameters,
    ithIndex);
   UnaryExpression unboxedIthParameter = Expression.Convert(
    ithParameter,
    parametersInfos[i].ParameterType
    );
   parameterExpressions[i] = unboxedIthParameter;
  }
 }

 NewExpression constructorCallExp = Expression.New(
  constructorInfo,
  parameterExpressions);

var convertResultExp = Expression.Convert(
  constructorCallExp,
 typeof(object)
  );

var constructorHandler = Expression
  .Lambda<ConstructorDelegate>(
   convertResultExp,
   constructorParameters
   )
  .Compile();

return constructorHandler;
}
object Func<object[]>
public delegate object? MethodDelegate(object instance, object[]? Args);

public static MethodDelegate GetMethodWithArgsDelegate(
    MethodInfo methodInfo,
    Type instanceType)
{
 ParameterExpression instanceParameter = Expression.Parameter(typeof(object));
 ParameterExpression methodParameters = Expression.Parameter(typeof(object?[]));

 Expression instanceExp;
 Expression[] parameterExpressions;
 {
  instanceExp = Expression.Convert(instanceParameter, instanceType);

  ParameterInfo[] parametersInfos = methodInfo.GetParameters();
  parameterExpressions = new Expression[parametersInfos.Length];

 for (int i = 0; i < parametersInfos.Length; i++)
  {
   ConstantExpression ithIndex = Expression.Constant(i);
   BinaryExpression ithParameter = Expression.ArrayIndex(
    methodParameters,
    ithIndex);
   UnaryExpression unboxedIthParameter = Expression.Convert(
    ithParameter,
    parametersInfos[i].ParameterType
    );
   parameterExpressions[i] = unboxedIthParameter;
  }
 }

var callExpression = Expression.Call(
  instanceExp,
  methodInfo,
  parameterExpressions
  );

var convertResultExp = Expression.Convert(
  callExpression,
 typeof(object)
  );

var callHandler = Expression
  .Lambda<MethodDelegate>(
   convertResultExp,
   instanceParameter,
   methodParameters
   )
  .Compile();

return callHandler;
}
object? Func<object, object[]>

 

Generic enum to int 

Теги: